package cm309.BookingSystem.tm.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cm309.BookingSystem.common.Common;
import cm309.BookingSystem.common.MyConnection;
import cm309.BookingSystem.tm.model.TM_Booking;
import cm309.BookingSystem.tm.model.TM_FreeTimeTable;
import cm309.BookingSystem.web.Action;

public class ReadBKAction implements Action {
	List<TM_Booking> bks=new ArrayList<TM_Booking>();
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		MyConnection con=new MyConnection();
		try {
			
			ResultSet rs=con.getPS(
					"select a.TM_BOOKINGID ,a.TM_BOOKINGID_1 ,a.TRAINERID ,a.MEMBERID ,a.RANGEID "
					+",a.STATE ,a.REMARK ,a.BONUSPOINT ,a.COMFIRM_EMAIL ,b.TIMEFROM ,b.TIMETO "
					+" from BK_TM_BOOKING a, BK_TM_FREETIMETABLE b where a.RANGEID =b.RANGEID ")
			               .executeQuery();
			
			TM_Booking bk;
			bks.clear();
			while (rs.next()) {
			bk=new TM_Booking();
			bk.setTm_BookingID(rs.getInt("TM_BOOKINGID"));
			bk.setTm_BookingID_1(rs.getString("TM_BOOKINGID_1"));
			bk.setTrainerID(rs.getString("TRAINERID"));
			bk.setMemberID(rs.getInt("MEMBERID"));
			bk.setRangeID(rs.getInt("RANGEID"));
			bk.setState(rs.getString("STATE"));
			bk.setRemark(rs.getString("REMARK"));
			bk.setBonusPoint(rs.getInt("BONUSPOINT"));
			bk.setComfirm_Email(rs.getString("COMFIRM_EMAIL"));
			bk.setStart(rs.getString("TIMEFROM"));
			bk.setEnd(rs.getString("TIMETO"));
				
		    bks.add(bk);
			
			}
			con.commit();
            con.PSclose();
			PrintWriter w = response.getWriter();
			
			w.println(Common.beanToJson(bks));
			
			
		} catch (IOException e) {
			e.printStackTrace();
		
	    } catch (SQLException e) {
	  	
	    	e.printStackTrace();
	    }
		finally {
		  con.close();
		}

	}

}
